{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!
Generated Cpp2 <-> Python converters for Thrift types
}}
{{> common/auto_generated_cython}}

from cpython.ref cimport PyObject
from libcpp.utility cimport move as __move
from folly.iobuf cimport (
    from_unique_ptr as __IOBuf_from_unique_ptr,
    IOBuf as __IOBuf,
)
from thrift.python.serializer import (
    deserialize as __deserialize,
    Protocol as __Protocol,
    serialize_iobuf as __serialize_iobuf,
)
import {{program:module_path}}.thrift_types as __thrift_types

{{#program:structs}}
cdef api int can_extract__{{program:capi_module_prefix}}__{{struct:py_name}}(object __obj) except -1:
    return 1 if isinstance(__obj, __thrift_types.{{struct:py_name}}) else 0

{{^struct:marshal_capi?}}
cdef api __cIOBuf* extract__{{program:capi_module_prefix}}__{{struct:py_name}}(object __obj) except NULL:
    cdef __IOBuf __buf = __serialize_iobuf(__obj, protocol=__Protocol.BINARY)
    return __buf._ours.release()

cdef api object construct__{{program:capi_module_prefix}}__{{struct:py_name}}(__unique_ptr[__cIOBuf] __s):
    return __deserialize(
        __thrift_types.{{struct:py_name}},
        __IOBuf_from_unique_ptr(__move(__s)),
        protocol=__Protocol.BINARY
    )
{{/struct:marshal_capi?}}

cdef api object init__{{program:capi_module_prefix}}__{{struct:py_name}}(object data):
    return __thrift_types.{{struct:py_name}}._fbthrift_create(data)

{{/program:structs}}
{{#program:enums}}
cdef api int can_extract__{{program:capi_module_prefix}}__{{enum:name}}(object __obj) except -1:
    return 1 if isinstance(__obj, __thrift_types.{{enum:name}}) else 0

cdef api object construct__{{program:capi_module_prefix}}__{{enum:name}}(int64_t __val):
    return __thrift_types.{{enum:name}}(__val)

{{/program:enums}}
